﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ReticleTrigger_for_ButtonStartPlay : MonoBehaviour {
    private Sprite normalImage;//原来的精灵图片
	public Sprite overImage;//准星经过时精灵图片	

	private VRReticleTriggerItem vrReticleTriggerItem;//处理准星精度条触发的类变量
	private void Awake ()
	{   //要保证对象也有VRReticleTriggerItem脚本
		vrReticleTriggerItem = GetComponent<VRReticleTriggerItem> () ?? gameObject.AddComponent<VRReticleTriggerItem>() ; 
        normalImage = GetComponent<Image> ().sprite;//保存原来的精灵图片
	}

	//对象使能（激活）时调用
	private void OnEnable()
	{	//订阅事件
		vrReticleTriggerItem.OnTrigger += HandleTrigger;
        vrReticleTriggerItem.OnOver += HandleOver;
        vrReticleTriggerItem.OnOut += HandleOut;
	}

	//对象不使能（不激活）时调用
	private void OnDisable()
	{	//去掉订阅的事件，与OnEnable的语句对应
		vrReticleTriggerItem.OnTrigger -= HandleTrigger;
        vrReticleTriggerItem.OnOver -= HandleOver;
		vrReticleTriggerItem.OnOut  -= HandleOut;	
	}

	//准星凝视进度条满了后，要触发的事件代码
	void HandleTrigger ()
	{  //设置当前对象父对象不活动（不可见，不可交互）
        this.transform.parent.gameObject.SetActive(false); 
        Debug.Log("你触发了开始按钮");//后续程序逻辑可在这里继续编码

		//调用游戏逻辑控制脚本中对应的public方法,开始游戏
        FindObjectOfType<GameManager>().StartPlay();

	}

    //准星移出对象时调用
	void HandleOut ()
	{
		this.gameObject.GetComponent<Image> ().sprite = normalImage;
	}

	//准星在对象上时调用
	void HandleOver ()
	{
		this.gameObject.GetComponent<Image> ().sprite = overImage;
    }

}

